package br.com.portalecbahia;

import java.util.ArrayList;
import java.util.List;

import com.google.analytics.tracking.android.EasyTracker;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.Toast;
import br.com.portalecbahia.connectionservices.SynchronizeRanking;
import br.com.portalecbahia.customize.PullToRefreshListView;
import br.com.portalecbahia.customize.PullToRefreshListView.OnRefreshListener;
import br.com.portalecbahia.customize.RankingListAdapter;
import br.com.portalecbahia.dao.RankingDAO;
import br.com.portalecbahia.listner.RankingListeners;
import br.com.portalecbahia.model.Championship;
import br.com.portalecbahia.model.Ranking;
import br.com.portalecbahia.util.PortalECBahiaUtil;

public class RankingActivity  extends ListActivity implements RankingListeners{
	
	private Context context;
	private List<Ranking> lRanking;
	private ListAdapter adapterListRanking;
	private SynchronizeRanking taskRanking;
	private Championship currentType ;
	private Button baianoBTN;
	private Button brasileiroBTN;
	private Button nordesteBTN;
	private ImageView updateIMG;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ranking);
        context = this;
        EasyTracker.getInstance().setContext(this);
        baianoBTN = (Button) findViewById(R.id.baianoBTN);
        brasileiroBTN = (Button) findViewById(R.id.brasileiroBTN);
        nordesteBTN = (Button) findViewById(R.id.nordesteBTN);
        updateIMG = (ImageView) findViewById(R.id.updateIMG);

        
        ((PullToRefreshListView) getListView()).setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
            	if(PortalECBahiaUtil.haveNetworkConnection(context)){
            		taskRanking = new SynchronizeRanking(context);
            		taskRanking.execute(currentType);
            	}else{
        			Toast.makeText(context, getString(R.string.internetOff), Toast.LENGTH_LONG).show();
        			  ((PullToRefreshListView) getListView()).onRefreshComplete();
            	}
            }
        });
        currentType = new Championship();
        currentType.setId(1);
        listRanking();
       updateList(new View(this));
       

    }
    
    @Override
    public void onStart() {
      super.onStart();
      EasyTracker.getInstance().activityStart(this); // Add this method.
    }

    @Override
    public void onStop() {
      super.onStop();
      EasyTracker.getInstance().activityStop(this); // Add this method.
    }
	
	

	@Override
	public void onSynchronizeRankingComplete(Boolean response) {
		if(response){
			listRanking();
		}else{
			Toast.makeText(context, getString(R.string.serverProblem), Toast.LENGTH_LONG).show();
		}
	    updateIMG.clearAnimation();
	    ((PullToRefreshListView) getListView()).onRefreshComplete();
		
	}
	
	public void updateList(View view){
		if(PortalECBahiaUtil.haveNetworkConnection(context)){
			taskRanking = new SynchronizeRanking(context);
			taskRanking.execute(currentType);
			animateUpdate();
    	}else{
			Toast.makeText(context, getString(R.string.internetOff), Toast.LENGTH_LONG).show();
			  ((PullToRefreshListView) getListView()).onRefreshComplete();
    	}

	}

	private void animateUpdate() {
		int   currentRotation = 0;
		RotateAnimation anim = new RotateAnimation(currentRotation, (360*4),
		Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);
		currentRotation = (currentRotation + 45) % 360;
		anim.setInterpolator(new LinearInterpolator());
		anim.setDuration(4000);
		anim.setRepeatMode(Animation.INFINITE);
		anim.setRepeatCount(Animation.INFINITE);
		anim.setFillEnabled(true);
		anim.setFillAfter(true);
		updateIMG.startAnimation(anim);
		
	}
	private void listRanking() {
	   	lRanking = new ArrayList<Ranking>();
    	lRanking.clear();
    	
    	
    	//type Video
    	if(currentType.getId() == 4){
        	lRanking = new RankingDAO(this).findByChampionship(currentType);   
        	int sizeList = lRanking.size();
        	if(lRanking.size()<10){    		
        		for (int i = 0; i < PortalECBahiaUtil.minimumListRanking-sizeList; i++) {
        			lRanking.add(new Ranking());
    			}
        	}
        	baianoBTN.setBackgroundResource(R.drawable.left_bluebtn);
        	baianoBTN.setTextColor(getResources().getColor(R.color.white));
        	
        	brasileiroBTN.setBackgroundResource(R.drawable.center_bluebtn_selected);
        	brasileiroBTN.setTextColor(getResources().getColor(R.color.gray));
        	
        	nordesteBTN.setBackgroundResource(R.drawable.right_bluebtn_selected);
        	nordesteBTN.setTextColor(getResources().getColor(R.color.gray));
        	
		}else if(currentType.getId() == 1){
        	lRanking = new RankingDAO(this).findByChampionship(currentType);   
			int sizeList = lRanking.size();
	    	if(lRanking.size()<10){    		
	    		for (int i = 0; i < PortalECBahiaUtil.minimumListRanking-sizeList; i++) {
	    			lRanking.add(new Ranking());
				}
	    	}
	      
	    	baianoBTN.setBackgroundResource(R.drawable.left_bluebtn_selected);
        	baianoBTN.setTextColor(getResources().getColor(R.color.gray));
        	
        	brasileiroBTN.setBackgroundResource(R.drawable.center_bluebtn);
        	brasileiroBTN.setTextColor(getResources().getColor(R.color.white));
        	
        	nordesteBTN.setBackgroundResource(R.drawable.right_bluebtn_selected);
        	nordesteBTN.setTextColor(getResources().getColor(R.color.gray));
        	        	
		}else if(currentType.getId() == 2){
        	lRanking = new RankingDAO(this).findByChampionship(currentType);   
			int sizeList = lRanking.size();
	    	if(lRanking.size()<10){    		
	    		for (int i = 0; i < PortalECBahiaUtil.minimumListRanking-sizeList; i++) {
	    			lRanking.add(new Ranking());
				}
	    	}
	      
	    	baianoBTN.setBackgroundResource(R.drawable.left_bluebtn_selected);
        	baianoBTN.setTextColor(getResources().getColor(R.color.gray));
        	
        	brasileiroBTN.setBackgroundResource(R.drawable.center_bluebtn_selected);
        	brasileiroBTN.setTextColor(getResources().getColor(R.color.gray));
        	
        	nordesteBTN.setBackgroundResource(R.drawable.right_bluebtn);
        	nordesteBTN.setTextColor(getResources().getColor(R.color.white));
        	        	
		}
    	adapterListRanking = (ListAdapter) new RankingListAdapter(this, lRanking);

	    setListAdapter(adapterListRanking);
	  
	}
	
	public void listHome(View view){
		currentType.setId(4);
		listRanking();
	}
	public void listAll(View view){
		currentType.setId(1);
		listRanking();
	}
	
	
	
	public void listLive(View view){
		currentType.setId(2);
		listRanking();
	}

	
	
	
    
}
